package api;

import com.google.gson.Gson;
import service.TipService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/api/tip/*")
public class Tipapi extends HttpServlet {
    private TipService tipService = new TipService();

    private void doAllRequest(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        HttpSession session = req.getSession();
        session.setAttribute("uid","123456");
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/json;charset=UTF-8");
        PrintWriter out = null;
        // 1获取/api/course/*的*所代表的方法
        // 获取请求URL的后缀
        String pathInfo = req.getPathInfo();
        // 获取URL后缀对应的方法名
        String methodName = pathInfo.substring(1);

        //通过反射机制找到对应的方法
        try {
            out = resp.getWriter();
            Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            //  利用反射调用对应的方法
            method.invoke(this, req, resp);
        } catch (Exception e) {
            e.printStackTrace();
            out.println("failed");
            out.close();
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doAllRequest(req, resp);
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doAllRequest(req, resp);
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doAllRequest(req, resp);
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doAllRequest(req, resp);
    }

    /*
     * servlet 方法合集
     * */

    //提供给商家端的，返回给商家所有订单信息json格式
    public void getTips(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        PrintWriter out = resp.getWriter();
        String sql = "select * from tipstable";
        System.out.println("执行了gettips方法");
        List tips = tipService.getTip(sql);
        out.println(new Gson().toJson(tips));
    }

    //提供给用户端的,req包含用户id，返回json格式订单数据。
    public void getMytip(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        PrintWriter out = resp.getWriter();
        String sql = "select * from tipstable where userId = ?";
        String uid = (String) req.getSession().getAttribute("uid");
        System.out.println("执行了getMytip方法");
        List tips = tipService.getTip(sql, uid);
        out.println(new Gson().toJson(tips));
    }

    //用户下单后，执行该方法，存储订单
    public void addtip(
            HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        PrintWriter out = resp.getWriter();
        String sql = "insert into tipstable(userId,seat,orderTime,money,status,details)values(?,?,now(),?,'未处理',?)";
        System.out.println("执行了addtip方法");
        String uid = "123457";
        String seat = "0";
        String money = "12312";
        String details = "米饭:1,烤鱼:1";

        out.println(tipService.addtip(sql, uid, seat, money,details));
    }

    //商家可以修改订单状态
    public void updattip(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        PrintWriter out = resp.getWriter();
        String sql = "update tipstable set status = ? where orderNum = ?";
        String orderNum = req.getParameter("orderNum");
        String status =  req.getParameter("status");
        System.out.println(status);
        System.out.println("执行了updattip方法");
        out.println(tipService.updattip(sql, orderNum, status));

    }

    //商家可以删除订单
    public void deletetip(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException{
        PrintWriter out = resp.getWriter();
        String sql = "delete from tipstable where orderNum = ? ";
        String orderNum = "10000";
        System.out.println("执行了deletetip方法");
        out.println(tipService.deletetip(sql, orderNum));
    }
}
